﻿using System.Collections.Generic;
using System.Linq;
using EngineDrawer;
using Microsoft.Xna.Framework;
using ThinkDotNetBotApiCommon;
using ThinkDotNetBotApiCommon.Weapons;
using ThinkDotNetEngine;
using ThinkDotNetEngine.Bots;
using ThinkDotNetEngine.Bots.Ammos;
using ThinkDotNetEngine.Bots.Loaders;
using ThinkDotNetEngine.Bots.Weapons;
using ThinkDotNetEngine.StaticObjects;
using ThinkDotNetEngineDrawer.Bots;

namespace ThinkDotNetEngineDrawer
{
    public class ArenaDrawer:WorldDrawer
    {
        readonly  private IDictionary<TankBot,BotDrawer> BotDrawersMappings=new Dictionary<TankBot, BotDrawer>();

        public ArenaDrawer(Game game,string worldMapPath):base(game,worldMapPath)
        {     
        }
        public override void Initialize()
        {            
            base.Initialize();            
        }
        protected override void LoadContent()
        {
            base.LoadContent();

            Game.Services.AddService(typeof(IAmmoHandler), new AmmoHandler(World));
            Game.Services.AddService(typeof(IAmmoLoader), new CachedAmmoLoader(Game));            

            InitBots();
        }
        private void InitBots()
        {
            IBotLoader botLoader = new AssemblyBotLoader();
            Arena.Bots = new List<TankBot>();
            BotHq[] botsHq = Arena.StaticObjects.Where(s => s is BotHq).Cast<BotHq>().ToArray();
            int botsHqIndex = 0;

            foreach (BotBase bot in botLoader.GetAllBots())
            {
                if(botsHqIndex>=botsHq.Length)
                    break;

                var tankBot = new TankBot(bot, Arena,botsHq[botsHqIndex++].Translation);
                tankBot.Destroyed += TankBotDestroyed;
                Arena.Bots.Add(tankBot);

                var weaponLoader = new CachedWeaponLoader(Game);
                tankBot.AllWeapons.Add(CannonWeaponType.Plasma, weaponLoader.Load(tankBot, WeaponType.Plasma));
                tankBot.AllWeapons.Add(CannonWeaponType.Rocket, weaponLoader.Load(tankBot, WeaponType.Rocket));
                tankBot.TacticalMissileWeapon = weaponLoader.Load(tankBot, WeaponType.Missile);
                tankBot.ChangeWeapon(CannonWeaponType.Plasma);
                BotController botController = (BotController) bot.BotController;
                botController.Init(tankBot);

                var botDrawer = new BotDrawer(Game, tankBot);
                Game.Components.Add(botDrawer);
                BotDrawersMappings.Add(tankBot,botDrawer);
            }
        }
        void TankBotDestroyed(object sender, System.EventArgs e)
        {
            var bot = (TankBot) sender;
            Arena.Bots.Remove(bot);
            BotDrawer botDrawer = BotDrawersMappings[bot];
            Game.Components.Remove(botDrawer);
            BotDrawersMappings.Remove(bot);
        }
        protected override Engine.World CreateWorldObject()
        {
            return new Arena(Game);
        }
        public Arena Arena
        {
            get { return (Arena) World; }
        }        
    }
}
